package com.xhcl.system.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.xhcl.common.core.domain.Stree;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xhcl.system.mapper.TGgDwgjMapper;
import com.xhcl.system.mapper.TGgFsdwMapper;
import com.xhcl.system.mapper.TGgPtdwgjMapper;
import com.xhcl.system.domain.TGgDwgj;
import com.xhcl.system.domain.TGgFsdw;
import com.xhcl.system.domain.TGgPtdwgj;
import com.xhcl.system.service.ITGgFsdwService;
import com.xhcl.common.core.text.Convert;

/**
 * 非税单位Service业务层处理
 * 
 * @author xhcl
 * @date 2020-08-25
 */
@Service
public class TGgFsdwServiceImpl implements ITGgFsdwService 
{
    @Autowired
    private TGgFsdwMapper tGgFsdwMapper;
    
    @Autowired
    private TGgDwgjMapper tGgDwgjMapper;
    
    @Autowired
    private TGgPtdwgjMapper tGgPtdwgjMapper;

    /**
     * 查询非税单位
     * 
     * @param dwbm 非税单位ID
     * @return 非税单位
     */
    @Override
    public TGgFsdw selectTGgFsdwById(String dwbm)
    {
        return tGgFsdwMapper.selectTGgFsdwById(dwbm);
    }

    /**
     * 查询非税单位列表
     * 
     * @param tGgFsdw 非税单位
     * @return 非税单位
     */
    @Override
    public List<TGgFsdw> selectTGgFsdwList(TGgFsdw tGgFsdw)
    {
        return tGgFsdwMapper.selectTGgFsdwList(tGgFsdw);
    }

    /**
     * 新增非税单位
     * 
     * @param tGgFsdw 非税单位
     * @return 结果
     */
    @Override
    public int insertTGgFsdw(TGgFsdw tGgFsdw)
    {
        return tGgFsdwMapper.insertTGgFsdw(tGgFsdw);
    }

    /**
     * 修改非税单位
     * 
     * @param tGgFsdw 非税单位
     * @return 结果
     */
    @Override
    public int updateTGgFsdw(TGgFsdw tGgFsdw)
    {
        return tGgFsdwMapper.updateTGgFsdw(tGgFsdw);
    }

    /**
     * 删除非税单位对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTGgFsdwByIds(String ids)
    {
        return tGgFsdwMapper.deleteTGgFsdwByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除非税单位信息
     * 
     * @param dwbm 非税单位ID
     * @return 结果
     */
    @Override
    public int deleteTGgFsdwById(String dwbm)
    {
        return tGgFsdwMapper.deleteTGgFsdwById(dwbm);
    }

    /**
     * 查询非税单位树列表
     * 
     * @return 所有非税单位信息
     */
    @Override
    public List<Stree> selectTGgFsdwTree()
    {
        List<TGgFsdw> tGgFsdwList = tGgFsdwMapper.selectTGgFsdwList(new TGgFsdw());
        List<Stree> ztrees = new ArrayList<Stree>();
        for (TGgFsdw tGgFsdw : tGgFsdwList)
        {
            Stree ztree = new Stree();
            ztree.setId(tGgFsdw.getDwbm());
            ztree.setpId(tGgFsdw.getSjdwbm());
            ztree.setName(tGgFsdw.getDwbm()+"-"+tGgFsdw.getDwmc());
            ztree.setTitle(tGgFsdw.getDwmc());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    
    @Override
    public List<Stree> selectTGgFsdwTreeforGjzt(String gjzt)
    {
    	TGgFsdw ggfsdw=new TGgFsdw();
    	if (StringUtils.isNotEmpty(gjzt)) {
    		ggfsdw.getParams().put("gjzt", gjzt);
    	}
        List<TGgFsdw> tGgFsdwList = tGgFsdwMapper.selectTGgFsdwList(ggfsdw);
        List<Stree> ztrees = new ArrayList<Stree>();
        for (TGgFsdw tGgFsdw : tGgFsdwList)
        {
            Stree ztree = new Stree();
            ztree.setId(tGgFsdw.getDwbm());
            ztree.setpId(tGgFsdw.getSjdwbm());
            ztree.setName(tGgFsdw.getDwbm()+"-"+tGgFsdw.getDwmc());
            ztree.setTitle(tGgFsdw.getDwmc());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    public List<Stree> selectTGgFsdwTree(String dwbm)
    {   
    	List<TGgDwgj> list=null;
    	if (StringUtils.isNotEmpty(dwbm)) {
	    	TGgDwgj tGgDwgj=new TGgDwgj();
	    	tGgDwgj.setDwbm(dwbm);
	    	list=tGgDwgjMapper.selectTGgDwgjList(tGgDwgj);
    	}
	    List<TGgFsdw> tGgFsdwList = tGgFsdwMapper.selectTGgFsdwList(new TGgFsdw());
	    List<Stree> ztrees = new ArrayList<Stree>();
	    for (TGgFsdw tGgFsdw : tGgFsdwList)
	    {
	        Stree ztree = new Stree();
	        ztree.setId(tGgFsdw.getDwbm());
	        ztree.setpId(tGgFsdw.getSjdwbm());
	        ztree.setName(tGgFsdw.getDwbm()+"-"+tGgFsdw.getDwmc());
	        ztree.setTitle(tGgFsdw.getDwmc());
	        if (list!=null) {
	        	ztree.setChecked(list.stream().filter(m->m.getFsdwbm().equals(tGgFsdw.getDwbm())).count()>0);
	        }
	        ztrees.add(ztree);
	    }
	    return ztrees;
    }
    
    public List<Stree> selectPtGgFsdwTree(String dwbm)
    {   
    	List<TGgPtdwgj> list=null;
    	if (StringUtils.isNotEmpty(dwbm)) {
    		TGgPtdwgj tGgDwgj=new TGgPtdwgj();
	    	tGgDwgj.setPtdwbm(dwbm);
	    	list=tGgPtdwgjMapper.selectTGgPtdwgjList(tGgDwgj);
    	}
	    List<TGgFsdw> tGgFsdwList = tGgFsdwMapper.selectTGgFsdwList(new TGgFsdw());
	    List<Stree> ztrees = new ArrayList<Stree>();
	    for (TGgFsdw tGgFsdw : tGgFsdwList)
	    {
	        Stree ztree = new Stree();
	        ztree.setId(tGgFsdw.getDwbm());
	        ztree.setpId(tGgFsdw.getSjdwbm());
	        ztree.setName(tGgFsdw.getDwbm()+"-"+tGgFsdw.getDwmc());
	        ztree.setTitle(tGgFsdw.getDwmc());
	        if (list!=null) {
	        	ztree.setChecked(list.stream().filter(m->m.getDwbm().equals(tGgFsdw.getDwbm())).count()>0);
	        }
	        ztrees.add(ztree);
	    }
	    return ztrees;
    }
}
